package com.yy.myp.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Room;

import com.yy.myp.R;
import com.yy.myp.common.CommonConstant;
import com.yy.myp.database.AppDatabase;
import com.yy.myp.dto.LocalPositionDto;
import com.yy.myp.entity.Point;
import com.yy.myp.entity.PointCard;

import java.util.ArrayList;
import java.util.List;

public class MainCardItemAdapter extends RecyclerView.Adapter<MainCardItemAdapter.MainCardItemAdapterHolder> {

    private List<PointCard> positions;

    private Context context;

    private int selectPosition = -1;

    private AppDatabase db;

    private OnItemClickListener onItemClickListener;

    public MainCardItemAdapter (Context context) {
        super();
        this.context = context;
        this.positions = new ArrayList<>();
        initLocalData();
    }

    private void initLocalData() {
        db = Room.databaseBuilder(this.context, AppDatabase.class, CommonConstant.DB_NAME).allowMainThreadQueries().build();
        List<LocalPositionDto> localPositionDtos = db.positionDao().selectAll();
        for (LocalPositionDto localPositionDto : localPositionDtos) {
            PointCard point = new PointCard(localPositionDto.getPosName(), localPositionDto.getLatitude(), localPositionDto.getLongitude());
            point.setBitmap(localPositionDto.getBitmap());
            this.positions.add(point);
        }
    }

    @NonNull
    @Override
    public MainCardItemAdapterHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_main_card_item, parent, false);
        MainCardItemAdapterHolder holder = new MainCardItemAdapterHolder(view);
        holder.itemView.setOnClickListener(v -> {
            int newSelPosition = holder.getBindingAdapterPosition();
            if (newSelPosition == selectPosition) {
                return;
            }
            notifyItemChanged(selectPosition);
            selectPosition = newSelPosition;
            notifyItemChanged(selectPosition);
            if (null != onItemClickListener) {
                onItemClickListener.onItemClick(positions.get(selectPosition));
            }
            Log.v("TAG", "I click position: " + selectPosition);
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MainCardItemAdapter.MainCardItemAdapterHolder holder, int position) {
        final int mPosition = position;

        PointCard point = positions.get(mPosition);
        holder.cardTitle.setText(point.getPosName());
        holder.cardPosition.setText(String.format("经度：%.8f  纬度: %.8f", point.getLatitude(), point.getLongitude()));
        holder.cardImg.setImageBitmap(point.getBitmap());

        if (mPosition == selectPosition) {
            holder.cardView.setBackgroundColor(ContextCompat.getColor(context, R.color.btn_second));
            holder.cardTitle.setTextColor(Color.WHITE);
            holder.cardPosition.setTextColor(Color.WHITE);
        } else {
            holder.cardView.setBackgroundColor(Color.WHITE);
            holder.cardTitle.setTextColor(Color.BLACK);
            holder.cardPosition.setTextColor(Color.BLACK);
        }

        holder.itemView.setSelected(mPosition == selectPosition);
//        holder.itemView.setOnClickListener( v -> {
//            selectPosition = mPosition;
//            notifyItemRangeChanged(0, getItemCount());
////            notifyDataSetChanged();
//            Log.v("TAG", "i click position: " + selectPosition);
//        });
//        holder.itemView.setOnClickListener(v -> {
//            Log.v("TAG", "我选择了: "+ mPosition);
//        });
    }

    public void addPosition (int localPosId) {
        LocalPositionDto localPositionDto = db.positionDao().selectById(localPosId);
        if (null != localPositionDto) {
            PointCard point = new PointCard(localPositionDto.getPosName(), localPositionDto.getLatitude(), localPositionDto.getLongitude());
            point.setBitmap(localPositionDto.getBitmap());
            this.positions.add(point);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return positions.size();
    }

    public void setOnItemClickListener (OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(PointCard pointCard);
    }

    class MainCardItemAdapterHolder extends RecyclerView.ViewHolder {

        private TextView cardTitle;

        private TextView cardPosition;

        private CardView cardView;

        private ImageView cardImg;

        public MainCardItemAdapterHolder(@NonNull View itemView) {
            super(itemView);
            this.cardTitle = itemView.findViewById(R.id.card_title);
            this.cardPosition = itemView.findViewById(R.id.card_position);
            this.cardView = itemView.findViewById(R.id.mt_card);
            this.cardImg = itemView.findViewById(R.id.card_img);
        }
    }

}
